﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace WebApp
{
    public partial class ManagerMain : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRead_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112102;uid=2014112102;pwd=19960523;";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open(); 
                string strSql = "select * from Members";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
                this.GridView1.DataSource = dr;
                this.GridView1.DataBind();
                dr.Close();
            }
            catch (Exception ex)
            {
                this.lblUserMes.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
            
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112102;uid=2014112102;pwd=19960523;";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("INSERT INTO Members ");
                SqlStringBuilder.Append("                (MemId, Name, Gender, Birth, Job, Salary, Tel, Address) ");
                SqlStringBuilder.Append("VALUES   (@MemId,@Name,@Gender,@Birth,@Job,@Salary,@Tel,@Address) ");

                string strSql = SqlStringBuilder.ToString();
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlParameter pmsMemId = new SqlParameter("@MemId", this.txtNo.Text);
                SqlParameter pmsName = new SqlParameter("@Name", this.txtName.Text);
                SqlParameter pmsGender = new SqlParameter("@Gender", this.DropDownList1.Text);
                SqlParameter pmsBirth = new SqlParameter("@Birth", this.txtBirth.Text);
                SqlParameter pmsJob = new SqlParameter("@Job", this.txtJob.Text);
                SqlParameter pmsSalary = new SqlParameter("@Salary", this.txtSal.Text);
                SqlParameter pmsTel = new SqlParameter("@Tel", this.txtTel.Text);
                SqlParameter pmsAddress = new SqlParameter("@Address", this.txtAddr.Text);
                cmd.Parameters.Add(pmsMemId);
                cmd.Parameters.Add(pmsName);
                cmd.Parameters.Add(pmsGender);
                cmd.Parameters.Add(pmsBirth);
                cmd.Parameters.Add(pmsJob);
                cmd.Parameters.Add(pmsSalary);
                cmd.Parameters.Add(pmsTel);
                cmd.Parameters.Add(pmsAddress);
                int iAdd = cmd.ExecuteNonQuery();
                if (iAdd < 1)
                {
                    this.lblUserMes.Text = "添加失败！";
                }
                else
                {
                    this.lblUserMes.Text = "添加了" + iAdd.ToString() + "条记录！";
                }
            }
            catch (Exception ex)
            {
                this.lblUserMes.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.lblNotice.Text = "查询信息可按职工姓名或职工编号查询";
        }

        protected void btnSearchOne_Click(object sender, EventArgs e)
        {
            string strSql = "SELECT   MemId, Name, Gender, Birth, Job, Salary, Tel, Address FROM  Members WHERE   (Name = @Name)";
            SqlParameter[] pms = new SqlParameter[1];
            pms[0] = new SqlParameter("@Name", this.txtName.Text);
            SqlDataReader dr = SqlHelper.ExecuteReader(strSql, pms);
            if (dr.Read())
            {
                this.txtNo.Text = dr["MemId"].ToString();
                this.DropDownList1.Text = dr["Gender"].ToString();
                this.txtBirth.Text = dr["Birth"].ToString();
                this.txtJob.Text = dr["Job"].ToString();
                this.txtTel.Text = dr["Tel"].ToString();
                this.txtSal.Text = dr["Salary"].ToString();
                this.txtAddr.Text = dr["Address"].ToString();
            } dr.Close();
        }

        protected void btnSearchTwo_Click(object sender, EventArgs e)
        {
            string strSql = "SELECT   MemId, Name, Gender, Birth, Job, Salary, Tel, Address FROM  Members WHERE   (MemId = @MemId)";
            SqlParameter[] pms = new SqlParameter[1];
            pms[0] = new SqlParameter("@MemId", this.txtNo.Text);
            SqlDataReader dr = SqlHelper.ExecuteReader(strSql, pms);
            if (dr.Read())
            {
                this.txtName.Text = dr["Name"].ToString();
                this.DropDownList1.Text = dr["Gender"].ToString();
                this.txtBirth.Text = dr["Birth"].ToString();
                this.txtJob.Text = dr["Job"].ToString();
                this.txtTel.Text = dr["Tel"].ToString();
                this.txtSal.Text = dr["Salary"].ToString();
                this.txtAddr.Text = dr["Address"].ToString();
            } dr.Close();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112102;uid=2014112102;pwd=19960523";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();
                string strSql = "UPDATE Members SET Name=N'" + this.txtName.Text + "',Gender=N'" + this.DropDownList1.Text + "',Birth=N'" + this.txtBirth.Text + "',Job=N'" + this.txtJob.Text + "',Salary=N'" + this.txtSal.Text + "',Tel=N'" + this.txtTel.Text + "',Address=N'" + this.txtAddr.Text + "'WHERE (MemId=N'" + this.txtNo.Text + "')";  
                SqlCommand cmd = new SqlCommand(strSql, cn);
                int iUpd = cmd.ExecuteNonQuery();
                if (iUpd < 1)
                {
                    this.lblUserMes.Text = "修改失败！";
                }
                else
                {
                    this.lblUserMes.Text = "修改了" + iUpd.ToString() + "条记录！";
                }
            }
            catch (Exception ex)
            {
                this.lblUserMes.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112102;uid=2014112102;pwd=19960523";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();
                string strSql = "DELETE FROM Members WHERE  (MemId = N'" + this.txtNo.Text + "')";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                int iDelete = cmd.ExecuteNonQuery();
                if (iDelete < 1)
                {
                    this.lblUserMes.Text = "删除失败！";
                }
                else
                {
                    this.lblUserMes.Text = "删除了" + iDelete.ToString() + "条记录！";
                }
            }
            catch (Exception ex)
            {
                this.lblUserMes.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            this.lblTime.Text = System.DateTime.Now.ToString();
        }

       

        
    }
}